home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 5 / CD-ROM Today - The Disc (Issue 5)(November 1994).ISO / mac / Mac shareware / Education / RLaB / rlib / std.r < prev    next >
Text File  |  1994-09-21  |  752b  |  31 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    std ( A )
  4.  
  5. //  Description:
  6.  
  7. //  Compute the Standard Deviation. For a vector (row-matrix), std()
  8. //  returns the standard deviation. For a MxN matrix, std() returns a
  9. //  row-matrix containing the standard deviation of each column.
  10.  
  11. //  See Also: mean, rand, srand
  12. //-------------------------------------------------------------------//
  13.  
  14. std = function(x)
  15. {
  16.   local(i, m, s);
  17.  
  18.   if(class(x) != "num") { error("std() requires NUMERICAL input"); }
  19.  
  20.   m = x.nr;
  21.   if( m == 1 ) 
  22.   { 
  23.     return sqrt( sum( (x - mean(x)) .^ 2 ) / (x.nc - 1) );
  24.   else
  25.     for( i in 1:x.nc) {
  26.       s[i] = sqrt( sum( (x[;i] - mean(x[;i])) .^ 2 ) / (x.nr - 1) );
  27.     }
  28.     return s;
  29.   }
  30. };
  31.